package com.apps.member.download.service.impl;

import com.apps.member.download.enums.DownloadAccountItemMinType;
import com.apps.member.download.enums.DownloadAccountItemType;
import com.apps.member.download.model.po.KupaMemberDownloadAccount;
import com.apps.member.download.dao.KupaMemberDownloadAccountMapper;
import com.apps.member.download.model.po.KupaMemberDownloadAccountItem;
import com.apps.member.download.service.IKupaMemberDownloadAccountItemService;
import com.apps.member.download.service.IKupaMemberDownloadAccountService;
import com.apps.sexctption.DataException;
import com.apps.sworld.dao.impl.sDaoImpl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大鹏
 * @since 2020-11-06
 */
@Service
public class KupaMemberDownloadAccountServiceImpl extends sDaoImpl<KupaMemberDownloadAccountMapper, KupaMemberDownloadAccount> implements IKupaMemberDownloadAccountService
{

    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private IKupaMemberDownloadAccountItemService downloadAccountItemService;

    @Override
    public KupaMemberDownloadAccount Income(Integer member_id, Integer count, DownloadAccountItemMinType itemMinType)
    {
        KupaMemberDownloadAccount account=getOneByMemberId(member_id,new KupaMemberDownloadAccount());
        KupaMemberDownloadAccountItem accountItem=new KupaMemberDownloadAccountItem();
        accountItem.setAccount_id(account.getId());
        accountItem.setMember_id(member_id);
        accountItem.setCount(count);
        accountItem.setType(DownloadAccountItemType.INCOME.name());
        accountItem.setType_min(itemMinType.name());
        accountItem.setBalance(account.getAvailable_balance());
        downloadAccountItemService.saveToDB(accountItem);
        try
        {
            account=applicationContext.getBean(this.getClass()).updateAccount(member_id,count);
        }catch (Exception e)
        {
            downloadAccountItemService.deleteById(accountItem.getId());
        }
        return account;
    }

    @Override
    public KupaMemberDownloadAccount Spend(Integer member_id, Integer count, DownloadAccountItemMinType itemMinType)
    {
        KupaMemberDownloadAccount account=getOneByMemberId(member_id,new KupaMemberDownloadAccount());
        KupaMemberDownloadAccountItem accountItem=new KupaMemberDownloadAccountItem();
        accountItem.setAccount_id(account.getId());
        accountItem.setMember_id(member_id);
        accountItem.setCount(count);
        accountItem.setType(DownloadAccountItemType.INCOME.name());
        accountItem.setType_min(itemMinType.name());
        accountItem.setBalance(account.getAvailable_balance());
        downloadAccountItemService.saveToDB(accountItem);
        try
        {
            account=applicationContext.getBean(this.getClass()).updateAccount(member_id,count * -1);
        }catch (Exception e)
        {
            downloadAccountItemService.deleteById(accountItem.getId());
        }
        return account;
    }

    @Override
    public KupaMemberDownloadAccount updateAccount(Integer member_id, Integer count)
    {
        KupaMemberDownloadAccount account = getOneByMemberId(member_id, new KupaMemberDownloadAccount());
        if (Integer.valueOf(account.getAvailable_balance()+count).intValue() <= 0)
            throw new DataException("可用余额不足");
        if (count.intValue() > 0)
            account.setTotal_income(account.getTotal_income()+count);
        else
            account.setTotal_spend(account.getTotal_spend()+(count * -1));

        //计算可用余额 (总收入-总支出)-冻结余额
        account.setAvailable_balance(account.getTotal_income()-account.getTotal_spend());
        return updateById(account);
    }
}
